<?php

namespace App\Http\Controllers\Managea;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\BannerRepository;
use App\Traits\BaseFun;

class BannerController extends Controller
{
	public function __construct(BannerRepository $BannerRepository)
    {
        $this->BannerRepository=$BannerRepository;
    }
    public function Banner(Request $request)
    {   
        $where=[];
        if ($request->search) {
            $where=['search'=>$request->search];
        }
        $res = $this->BannerRepository->list($where);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function BannerAdd(Request $request)
    {   
        $res = $this->BannerRepository->create($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '添加失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function BannerEdit(Request $request,$id)
    {
        $res=$this->BannerRepository->info(['id'=>$id]);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '获取失败!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function BannerUpdate(Request $request)
    {
        $res=$this->BannerRepository->update($request->all());
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '修改成功!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function BannerDel(Request $request,$id)
    {
        $res=$this->BannerRepository->delete($id);
        if ($res) {
           $ArrDate = ['state' => 0, 'data' => $res, 'token' => $request->input('token')];
        }else{
           $ArrDate = ['state' => 2, 'data' => $res, 'msg' => '删除失败或没有此数据!']; 
        }
        return BaseFun::jsonReturn($ArrDate);
    }
    public function BannerState(Request $request, $id)
    {
        $info = $this->BannerRepository->info(['id' => $id]);
        if ($info['state'] == 1) {
            # 封号 删除 token
            $param = ['state' => '2'];
        } elseif ($info['state'] == 2) {
            $param = ['state' => '1'];
        }
        $param['id'] = $id;

        $up = $this->BannerRepository->update($param);
        if ($up) {
            $ArrDate = ['state' => 0, 'data' => $up, 'token' => $request->input('token')];
        } else {
            $ArrDate = ['state' => 2, 'data' => '', 'msg' => '关闭或开启失败'];
        }

        return BaseFun::jsonReturn($ArrDate);
    }
   

}
